home *** CD-ROM | disk | FTP | other *** search
- /* ***** BEGIN LICENSE BLOCK *****
- *
- * Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France
- * (company in charge of developing Pearltrees)
- *
- * This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.
- *
- * Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the
- * terms of the GNU General Public License version 3 as published by the Free Software Foundation.
- *
- * Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
- * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
- * See the GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO.
- * If not, see <http://www.gnu.org/licenses/>
- *
- * ***** END LICENSE BLOCK ***** */
-
- // ///////////////////////////////////////////////////////////////////////////////
- // In button
- // ///////////////////////////////////////////////////////////////////////////////
-
- var BRO_inButtonController = {
-
- _selectionChanged : false,
- _selectedTree : null,
- _selectedHistory : null,
- _selectedNewHistory : null,
- IS_NEWMAP_BUTTON_IN_LIST : false,
- TREE_LIST_NEWMAP_INDEX : 0,
- MAX_CHARS_IN_BUTTON_LABEL : 14,
-
- getSelectedTree : function() {
- if(!this._selectedTree) return null;
-
- // Return tree object from the tree list
- var treeList = BRO_model.getTreeList();
- if (!treeList) {
- return this._selectedTree;
- }
-
- for (var i = 0; i < treeList.length; i++) {
- if (treeList[i].treeID == this._selectedTree.treeID) {
- return treeList[i];
- }
- }
- return null;
- },
-
- getSelectedHistory : function() {
- if(!this._selectedHistory) return null;
-
- // Return history object from the history list
- var historyList = BRO_model.getHistoryList();
- if (!historyList) {
- return this._selectedHistory;
- }
-
- for (var i = 0; i < historyList.length; i++) {
- if (historyList[i].id == this._selectedHistory.id) {
- return historyList[i];
- }
- }
- return null;
- },
-
- getSelectedNewHistory : function() {
- return this._selectedNewHistory;
- },
-
- initTreeList : function() {
- // backup from preferences
- var rootTree = BRO_toolbar.backupRootTreeFromPreferences();
- var dropZone = BRO_toolbar.backupDropZoneFromPreferences();
- var currentUser = BRO_toolbar.backupCurrentUserFromPreferences();
- BRO_model.updateCurrentUser(currentUser);
-
- if (rootTree && !this.getSelectedTree() && !this.getSelectedHistory() && !this._selectedNewHistory) {
- // In order to select the tree we create a treeList with only one item
- var treeList = new Array(rootTree, dropZone);
- BRO_model.setTreeList(treeList);
- BRO_windowManager.setTreeList(treeList);
- // we rebuild the visual list
- this.onTreeListLoaded();
- // we add a loading... message to the list
- this.appendLoadingMessageToTreeList();
- // we select the root tree in the list
- this.selectTree(rootTree.treeID, false, true);
- }else{
- // will refresh tree list label
- BRO_inButtonController.refreshTreeListVisualComponents();
- }
- },
-
- onSelectTreeListItem : function(event) {
- var treeListBox = document.getElementById('BRO_treeListBox');
- if(treeListBox.selectedIndex < 0) return;
-
- var selectedValue = this.decodeListBoxItemValue(treeListBox.selectedItem);
- var startHistoryAfterSelect = false;
- if (BRO_toolbar.getRecordingMode() == RECORING_MODE_CONTINUOUS
- && BRO_toolbar.isRecording
- && BRO_inButtonController.isNewSelectedValueChangeSelectedTreeOrHistory()) {
- startHistoryAfterSelect = true;
- } else if (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE
- && BRO_toolbar.isCurrentUrlRecorded()
- && BRO_inButtonController.isNewSelectedValueChangeSelectedTreeOrHistory()) {
- startHistoryAfterSelect = true;
- }
-
- // If click on a tree
- if (this.isSelectedListBoxItemIsTree()) {
- this.selectTree(selectedValue.treeID, startHistoryAfterSelect);
- BRO_inButtonController._selectionChanged = true;
- }
- // If click on a history
- else if (this.isSelectedListBoxItemIsHistory()) {
- this.selectHistory(selectedValue.id, startHistoryAfterSelect);
- BRO_inButtonController._selectionChanged = true;
- }
- // If create new map is now selected
- else if (BRO_inButtonController.IS_NEWMAP_BUTTON_IN_LIST && this.isSelectedListBoxItemIsCreateNewMap()) {
- BRO_inButtonController.showNameMapWindow();
- } else {
- this.backupSelection();
- }
- },
-
- closePopup : function() {
- var newButtonPopup = document.getElementById("BRO_newButtonPopup");
- if(newButtonPopup) {
- newButtonPopup.hidePopup();
- }
- var newTreePanel = document.getElementById('BRO_newTreePanel');
- if(newTreePanel) {
- newTreePanel.hidePopup();
- }
- },
-
- isDropZoneSelected:function() {
- var currentUser = BRO_model.getCurrentUser();
- var selectedTree = BRO_inButtonController.getSelectedTree();
- return (selectedTree && selectedTree.treeID == currentUser.dropZoneID);
- },
-
- isNewSelectedValueChangeSelectedTreeOrHistory : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- var selectedValue = this.decodeListBoxItemValue(treeListBox.selectedItem);
-
- if (this.isSelectedListBoxItemIsTree()) {
- return (!this.getSelectedTree() || this.getSelectedTree().treeID != selectedValue.treeID);
- } else if (this.isSelectedListBoxItemIsHistory()) {
- return (!this.getSelectedHistory() || this.getSelectedHistory().id != selectedValue.id);
- } else {
- return false;
- }
- },
-
- isSelectedListBoxItemIsCreateNewMap : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- var selectedValue = this
- .decodeListBoxItemValue(treeListBox.selectedItem);
-
- // Just checking index is not enaugh to detect user selection
- if (treeListBox.currentIndex == BRO_inButtonController.TREE_LIST_NEWMAP_INDEX
- && !selectedValue
- && (this.getSelectedTree() || this.getSelectedHistory() || this._selectedNewHistory)) {
- return true;
- } else {
- return false;
- }
- },
-
- isSelectedListBoxItemIsTree : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- var selectedValue = this.decodeListBoxItemValue(treeListBox.selectedItem);
- return (selectedValue && selectedValue.treeID && (!this.getSelectedTree() || selectedValue != this.getSelectedTree().treeID));
- },
-
- isSelectedListBoxItemIsHistory : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- var selectedValue = this.decodeListBoxItemValue(treeListBox.selectedItem);
- return (selectedValue && selectedValue.id && selectedValue.name);
- },
-
- onClickDefaultTreeListItem : function(event) {
- BRO_toolbar.onUseToolbar();
- // If create new map was already selected
- if (!BRO_inButtonController._selectionChanged) {
- BRO_inButtonController.showNameMapWindow();
- }
- BRO_inButtonController._selectionChanged = false;
- },
-
- onClickTreeListItem : function(event) {
- BRO_inButtonController.closePopup();
-
- if (BRO_toolbar.isPearltreesPublicUrl()) {
- BRO_toolbar.openSelectedTreeInCurrentTab(null, true);
- }
- BRO_inButtonController._selectionChanged = false;
- },
-
- onClickHistoryListItem : function(event) {
- BRO_inButtonController.closePopup();
-
- BRO_inButtonController._selectionChanged = false;
- },
-
- onNewTreePanelShowing:function() {
- this.closePopup();
- // Use small timeout whereas XBL setters don't work...
- // Don't know why yet :(
- BRO_tools.callWithDelay('BRO_inButtonController.initNewTreePanel()', 20);
- },
-
- initNewTreePanel:function() {
- var newTreePanelContent = document.getElementById('BRO_newTreePanelContent');
- newTreePanelContent.focus();
- newTreePanelContent.text = "";
- },
-
- onClickCancelNewTree : function () {
- BRO_inButtonController.closePopup();
- },
-
- onClickValidateNewTree : function () {
- var newTreeTitle = document.getElementById('BRO_newTreePanelContent').text;
- if(newTreeTitle && BRO_tools.trim(newTreeTitle) != "") {
- this.closePopup();
- }
- BRO_inButtonController.createNewTree(newTreeTitle);
- },
-
- createNewTree : function(newTreeTitle) {
- if(!newTreeTitle || BRO_tools.trim(newTreeTitle) == "") {
- return;
- }
-
- this._selectedNewHistory = new Object;
- this._selectedNewHistory.name = newTreeTitle;
- var startHistoryAfterSelect = false;
- if (BRO_toolbar.getRecordingMode() == RECORING_MODE_CONTINUOUS && BRO_toolbar.isRecording) {
- startHistoryAfterSelect = true;
- }
- else if (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE && BRO_toolbar.isCurrentUrlRecorded()) {
- startHistoryAfterSelect = true;
- }
- else if(this.forceStartHistoryAfterSelect) {
- startHistoryAfterSelect = true;
- }
- BRO_inButtonController.selectNewHistory(this._selectedNewHistory, startHistoryAfterSelect);
-
- if (!startHistoryAfterSelect) {
- BRO_model.start(this._selectedNewHistory.name);
- }
- BRO_toolbar.isRevealed = false;
- BRO_toolbar.showNameMapDoneWindow();
- },
-
- forceStartHistoryAfterSelect : false,
-
- showNameMapWindow : function(forceStartHistoryAfterSelect) {
- this.forceStartHistoryAfterSelect = forceStartHistoryAfterSelect;
- BRO_toolbar.showNameMapWindow();
- },
-
- selectTree : function(treeID, startHistoryAfterSelect, isInitializing) {
- this._selectedHistory = null;
- BRO_windowManager.setSelectedHistory(null);
- this._selectedNewHistory = null;
- BRO_windowManager.setSelectedNewHistory(null);
- var treeList = BRO_model.getTreeList();
- if (!treeList) {
- return;
- }
-
- var newSelectedTree = null;
- for (var i = 0; i < treeList.length; i++) {
- if (treeList[i].treeID == treeID) {
- newSelectedTree = treeList[i];
- break;
- }
- }
-
- var selectedTreeChanged = (!this.getSelectedTree() || !newSelectedTree
- || this.getSelectedTree().treeID != newSelectedTree.treeID
- || this.getSelectedTree().title != newSelectedTree.title
- || this.getSelectedTree().lastUpdate != newSelectedTree.lastUpdate);
- if(selectedTreeChanged) {
- this._selectedTree = newSelectedTree;
-
- // If we don't find the tree in the list we create a temporary item
- if (!this._selectedTree) {
- var treeObject = {};
- treeObject.treeID = treeID;
- var currentUser = BRO_model.getCurrentUser();
- if (currentUser) {
- treeObject.title = currentUser.username.toLowerCase();
- } else {
- treeObject.title = BRO_locale.getString('new.button.label');
- }
- this._selectedTree = treeObject;
- }
-
- this.closePopup();
-
- BRO_toolbar.treeSelectionChanged();
- }
-
- if(!isInitializing) {
- BRO_windowManager.setSelectedTree(this.getSelectedTree());
- }
-
- this.refreshTreeListVisualComponents(startHistoryAfterSelect);
-
- if (startHistoryAfterSelect) {
- BRO_ButtonsHandler.startOrContinueHistory();
- }
- },
-
- selectHistory : function(historyID, startHistoryAfterSelect) {
- this._selectedTree = null;
- BRO_windowManager.setSelectedTree(null);
- this._selectedNewHistory = null;
- BRO_windowManager.setSelectedNewHistory(null);
- var historyList = BRO_model.getHistoryList();
- if (!historyList) {
- return;
- }
-
- var newSelectedHistory = null;
- for (var i = 0; i < historyList.length; i++) {
- if (historyList[i].id == historyID) {
- newSelectedHistory = historyList[i];
- break;
- }
- }
-
- var selectedHistoryChanged = (!this.getSelectedHistory() || !newSelectedHistory
- || this.getSelectedHistory().id != newSelectedHistory.id
- || this.getSelectedHistory().name != newSelectedHistory.name
- || this.getSelectedHistory().lastUpdate != newSelectedHistory.lastUpdate);
-
- if (selectedHistoryChanged) {
- this._selectedHistory = newSelectedHistory;
-
- this.closePopup();
-
- BRO_toolbar.treeSelectionChanged();
- }
-
- BRO_windowManager.setSelectedHistory(this._selectedHistory);
-
- this.refreshTreeListVisualComponents(startHistoryAfterSelect);
-
- if (startHistoryAfterSelect) {
- BRO_ButtonsHandler.startOrContinueHistory();
- }
- },
-
- isTreeIdInTreeList : function(treeID) {
- var treeList = BRO_model.getTreeList();
- if (!treeList)
- return false;
- for (var i = 0; i < treeList.length; i++) {
- if (treeList[i].treeID == treeID) {
- return true;
- }
- }
- return false;
- },
-
- selectNewHistory : function(selectedNewHistory, startHistoryAfterSelect) {
- this._selectedTree = null;
- BRO_windowManager.setSelectedTree(null);
- this._selectedHistory = null;
- BRO_windowManager.setSelectedHistory(null);
- // BRO_toolbar.saveSelectedTreeIntoPreferences(null);
-
- this._selectedNewHistory = selectedNewHistory;
- BRO_windowManager.setSelectedNewHistory(selectedNewHistory);
- BRO_toolbar.treeSelectionChanged();
-
- this.refreshTreeListVisualComponents(startHistoryAfterSelect);
-
- var newButtonPopup = document.getElementById('BRO_newButtonPopup');
- newButtonPopup.hidePopup();
- if (startHistoryAfterSelect) {
- BRO_ButtonsHandler.startOrContinueHistory(forceStartNew = true);
- }
- },
-
- onClickRootTree: function () {
- var currentUser = BRO_model.getCurrentUser();
- var startHistoryAfterSelect = false;
- var selectionChanged = (currentUser.rootTreeID != this.getSelectedTree());
- if (BRO_toolbar.getRecordingMode() == RECORING_MODE_CONTINUOUS
- && BRO_toolbar.isRecording
- && selectionChanged) {
- startHistoryAfterSelect = true;
- } else if (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE
- && BRO_toolbar.isCurrentUrlRecorded()
- && selectionChanged) {
- startHistoryAfterSelect = true;
- }
-
- this.selectRootTree(startHistoryAfterSelect);
- BRO_inButtonController._selectionChanged = selectionChanged;
-
- this.closePopup();
-
- if (BRO_toolbar.isPearltreesPublicUrl()) {
- BRO_toolbar.openSelectedTreeInCurrentTab();
- }
- },
-
- selectRootTree : function(startHistoryAfterSelect) {
- var currentUser = BRO_model.getCurrentUser();
- if (currentUser) {
- this.selectTree(currentUser.rootTreeID, startHistoryAfterSelect);
- }
- },
-
- onClickDropZoneTree: function () {
- var currentUser = BRO_model.getCurrentUser();
- var startHistoryAfterSelect = false;
- if (BRO_toolbar.getRecordingMode() == RECORING_MODE_CONTINUOUS
- && BRO_toolbar.isRecording
- && currentUser.rootTreeID != this.getSelectedTree()) {
- startHistoryAfterSelect = true;
- } else if (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE
- && BRO_toolbar.isCurrentUrlRecorded()
- && currentUser.rootTreeID != this.getSelectedTree()) {
- startHistoryAfterSelect = true;
- }
-
- this.selectDropZoneTree(startHistoryAfterSelect);
- BRO_inButtonController._selectionChanged = true;
-
- this.closePopup();
-
- if (BRO_toolbar.isPearltreesPublicUrl()) {
- BRO_toolbar.openSelectedTreeInCurrentTab();
- }
- },
-
- selectDropZoneTree : function(startHistoryAfterSelect) {
- var currentUser = BRO_model.getCurrentUser();
- if(currentUser) {
- this.selectTree(currentUser.dropZoneID, startHistoryAfterSelect);
- }
- },
-
- refreshTreeListVisualComponents : function(isRecording) {
- var newButton = document.getElementById('BRO_newButton');
- if(!newButton) return;
- var currentUser = BRO_model.getCurrentUser();
- var label = "";
- var tooltip = "";
- if (this.getSelectedTree()) {
- if (currentUser && this.getSelectedTree().treeID == currentUser.dropZoneID) {
- label = BRO_locale.getString('treeList.dropzone.selected');
- } else {
- label = this.getSelectedTree().title;
- }
-
- if(currentUser && this.getSelectedTree().treeID == currentUser.rootTreeID) {
- tooltip = BRO_locale.getString('new.button.tooltip.base.rootTreeSelected');
- }
- else {
- var tooltipBase = (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE)
- ? BRO_locale.getString('new.button.tooltip.base.oneByOne')
- : BRO_locale.getString('new.button.tooltip.base.continuous');
- tooltip = tooltipBase + " \"" + label + "\"";
- }
- } else if (this.getSelectedHistory()) {
- label = this.getSelectedHistory().name;
- var tooltipBase = (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE)
- ? BRO_locale.getString('new.button.tooltip.base.oneByOne')
- : BRO_locale.getString('new.button.tooltip.base.continuous');
- tooltip= tooltipBase + " \""
- + this.getSelectedHistory().name + "\"";
- } else if (this._selectedNewHistory) {
- label = this._selectedNewHistory.name;
- var tooltipBase = (BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE)
- ? BRO_locale.getString('new.button.tooltip.base.oneByOne')
- : BRO_locale.getString('new.button.tooltip.base.continuous');
- tooltip = tooltipBase + " \"" + this._selectedNewHistory.name + "\"";
- } else if (currentUser) {
- var rootTreeName = currentUser.username.toLowerCase();
- label = rootTreeName;
- tooltip = BRO_locale.getString('new.button.tooltip.base.rootTreeSelected');
- } else {
- label = BRO_locale.getString('new.button.label');
- tooltip = "";
- }
- if(label.length > this.MAX_CHARS_IN_BUTTON_LABEL) {
- label = label.substr(0, this.MAX_CHARS_IN_BUTTON_LABEL)+"..";
- }
- newButton.label = label;
- newButton.tooltipText = tooltip;
- this.refreshTreeListTextHeader(isRecording);
- this.refreshTreeListHeight();
- },
-
- /**
- * TODO setting description.value does not render well. Should fix this.
- */
- refreshTreeListTextHeader : function(isRecording) {
- if (!isRecording) {
- isRecording = (BRO_toolbar.isRecording && BRO_toolbar.getRecordingMode() == RECORING_MODE_CONTINUOUS);
- }
- var newButtonMenuPopupTitle = document.getElementById("BRO_newButtonMenuPopupTitle");
- if(!newButtonMenuPopupTitle) return;
- newButtonMenuPopupTitle.removeChild(newButtonMenuPopupTitle.firstChild);
- var headerText = BRO_locale.getString('new.header');
- if (isRecording) {
- headerText = BRO_locale.getString('new.header.recording');
- }
- var textNode = document.createTextNode(headerText);
- newButtonMenuPopupTitle.appendChild(textNode);
- },
-
- onTreeListNotChanged : function() {
- this.backupSelection();
- },
-
- onTreeListLoaded : function() {
- this.refreshSelectedHistory();
- this.rebuildTreeListAndBackupSelection();
- },
-
- refreshSelectedHistory : function() {
- var historyList = BRO_model.getHistoryList();
- if (this._selectedNewHistory && !this.getSelectedHistory() && !this.getSelectedTree() && historyList) {
- // Find an history by name
- var historyListLength = historyList.length;
-
- for (var i = historyListLength - 1; i >= 0; i--) {
- if (this._selectedNewHistory && historyList[i].name == this._selectedNewHistory.name) {
- this._selectedHistory = historyList[i];
- this._selectedNewHistory = null;
- }
- }
- }
- },
-
- rebuildTreeListAndBackupSelection : function() {
- this.cleanTreeListBox();
- this.populateTreeListBox();
- this.backupSelection();
- },
-
- backupSelection : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- if(!treeListBox) return;
- var currentUser = BRO_model.getCurrentUser();
-
- // Handle tree case
- if (this.getSelectedTree()) {
- if(currentUser && this.getSelectedTree().treeID == currentUser.rootTreeID && this.getSelectedTree().pearlCount >= MAX_PEARLS_IN_TREE) {
- var selectedTreeID = currentUser.dropZoneID;
- this.selectDropZoneTree(false);
- }
- else{
- var selectedTreeID = this.getSelectedTree().treeID;
- }
- }
- // Handle history case
- else if (this.getSelectedHistory()) {
- var selectedHistoryID = this.getSelectedHistory().id;
- }
- // Handle new tree case
- else if (this._selectedNewHistory) {
- // @nothing to select
- }
- // If there is no tree selected we select the root tree
- else if(currentUser) {
- var selectedTreeID = currentUser.rootTreeID;
- this.selectRootTree(false);
- }
- var isRootTreeSelected = selectedTreeID && currentUser && selectedTreeID == currentUser.rootTreeID;
- var isDropZoneSelected = selectedTreeID && currentUser && selectedTreeID == currentUser.dropZoneID;
-
- // Backup selection in the treeList
- treeListBox.selectItem(-1);
- if(!isRootTreeSelected && !isDropZoneSelected) {
- if (selectedTreeID || selectedHistoryID) {
- var itemCount = treeListBox.itemCount;
- for (var i = 0; i < itemCount; i++) {
- var item = treeListBox.getItemAtIndex(i);
- var value = this.decodeListBoxItemValue(item);
-
- if ((selectedTreeID && value && value.treeID == selectedTreeID) || (selectedHistoryID && value && value.id == selectedHistoryID)) {
-
- if(treeListBox.selectedItem != item) {
- treeListBox.selectItem(item);
- }
- // Error raised by richtextbox using ensureIndexIsVisible & getNumberOfVisibleRows
- if(BRO_toolbar.isBrowserVersionGreaterOrEqual("3.0")) {
- treeListBox.ensureIndexIsVisible(i);
-
- var numVisibleRows = treeListBox.getNumberOfVisibleRows();
- if (i > numVisibleRows) {
- treeListBox.scrollToIndex(i - Math.round(numVisibleRows / 2) + 2);
- }
- }
- break;
- }
- }
- }
- }
-
- // Backup dropZone selection
- var dropZoneButton = document.getElementById('BRO_dropZoneButton');
- if (isDropZoneSelected) {
- dropZoneButton.className = 'BRO_treeListBoxSpecialItemSelected';
- } else {
- dropZoneButton.className = 'BRO_treeListBoxSpecialItem';
- }
- // Backup rootTree selection
- var rootTreeButton = document.getElementById('BRO_rootTreeButton');
-
- if (isRootTreeSelected) {
- rootTreeButton.className = 'BRO_treeListBoxSpecialItemSelected';
- } else {
- rootTreeButton.className = 'BRO_treeListBoxSpecialItem';
- }
- },
-
- appendLoadingMessageToTreeList : function() {
- this.appendItemToListBox('loading...');
- this.refreshTreeListVisualComponents(false);
- },
-
- cleanTreeListBox : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- if (!treeListBox)
- return;
- while (treeListBox.firstChild) {
- treeListBox.removeChild(treeListBox.firstChild);
- }
- },
-
- populateTreeSubList : function(event) {
- var parentMenupopup = event.target;
-
- while(parentMenupopup.firstChild) {
- parentMenupopup.removeChild(parentMenupopup.firstChild);
- }
-
- var treeList = BRO_model.getTreeList();
- if (!treeList) return;
- var listLength = treeList.length;
-
- for (var i = 0; i < listLength; i++) {
- if(treeList[i].parentID == parentMenupopup.treeID) {
- var hasChild = false;
- for (var j = 0; j < listLength; j++) {
- if(treeList[i].treeID == treeList[j].parentID) {
- hasChild = true;
- }
- }
- if(hasChild) {
- var menu = document.createElement('menu');
- menu.setAttribute('label', treeList[i].title);
- var menupopup = document.createElement('menupopup');
- menupopup.setAttribute('onpopupshowing','BRO_inButtonController.populateTreeSubList(event)');
- menupopup.treeID = treeList[i].treeID;
- menu.appendChild(menupopup);
- parentMenupopup.appendChild(menu);
- }
- else{
- var item = document.createElement('menuitem');
- item.setAttribute('label', treeList[i].title);
- parentMenupopup.appendChild(item);
- }
- }
- }
- },
-
- populateTreeListBox : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
-
- // Prototype new hierarchy popups
- if(!treeListBox) {
-
- var popup = document.getElementById('BRO_newButtonPopup');
- var treeList = BRO_model.getTreeList();
- if (!treeList) return;
- var listLength = treeList.length;
-
- while(popup.firstChild) {
- popup.removeChild(popup.firstChild);
- }
-
- for (var i = 0; i < listLength; i++) {
- // random parent ID
- var parentIndex = BRO_tools.getRandomNumber(listLength);
- treeList[i].parentID = treeList[parentIndex].treeID;
-
- if(treeList[i].parentID == 2) {
- var hasChild = false;
- for (var j = 0; j < listLength; j++) {
- if(treeList[i].treeID == treeList[j].parentID) {
- hasChild = true;
- }
- }
-
- if(hasChild) {
- var menu = document.createElement('menu');
- menu.setAttribute('label', treeList[i].title);
- var menupopup = document.createElement('menupopup');
- menupopup.setAttribute('onpopupshowing','BRO_inButtonController.populateTreeSubList(event)');
- menupopup.treeID = treeList[i].treeID;
- menu.appendChild(menupopup);
- popup.appendChild(menu);
- }else{
- var item = document.createElement('menuitem');
- item.setAttribute('label', treeList[i].title);
- popup.appendChild(item);
- }
- }
- }
-
- return;
- }
- var currentUser = BRO_model.getCurrentUser();
-
- // Add default "new map" item
- // var defaultItemLabel = BRO_locale.getString('treeList.default');
- // var defaultItem = this.appendItemToListBox(defaultItemLabel);
- // defaultItem.style.fontWeight = 'bold';
- // defaultItem.addEventListener('click',
- // BRO_inButtonController.onClickDefaultTreeListItem, false);
-
- // Add space separator
- // treeListBox.appendChild(this.createTreeListLineSeparator());
-
- // Add a text seperator
- // treeListBox.appendChild(this.createTreeListTextSeparator(BRO_locale.getString('treeList.pearltreePartTitle')));
-
- // Add space separator
- // treeListBox.appendChild(this.createTreeListSpacerSeparator(8));
-
- // Prepare fetching the list
- var treeList = BRO_model.getTreeList();
- if (!treeList)
- return;
- var listLength = treeList.length;
-
- // Find user root tree and refresh its title
- var currentUser = BRO_model.getCurrentUser();
- var rootTreeTitle = document.getElementById('BRO_rootTreeTitle');
- if (currentUser && currentUser.rootTreeID && rootTreeTitle) {
- for (var i = 0; i < listLength; i++) {
- if (treeList[i].treeID == currentUser.rootTreeID) {
- rootTreeTitle.value = treeList[i].title;
- break;
- }
- }
- }
-
- // Add space separator
- // treeListBox.appendChild(this.createTreeListSpacerSeparator(8));
-
- // Merge the list of current histories
- var historyList = BRO_model.getHistoryList();
-
- if (historyList && historyList.length > 0) {
- treeList = treeList.concat(historyList);
- }
- treeList.sort(BRO_inButtonController.sortTreeListItemByTitle);
-
- listLength = treeList.length;
- var isSelectedTreeInList = false;
-
- // Populate the rest of the list
- for (var i = 0; i < listLength; i++) {
- var item = null;
-
- // If it is a tree
- if (treeList[i].treeID) {
- // Add items that are not the dropZone
- if (!currentUser || (treeList[i].treeID != currentUser.rootTreeID && treeList[i].treeID != currentUser.dropZoneID)) {
- var title = treeList[i].title;
- if(treeList[i].pearlCount >= MAX_PEARLS_IN_TREE) {
- title = title + " (full)";
- }
- item = this.appendItemToListBox(title, treeList[i], true);
- item.addEventListener('click', BRO_inButtonController.onClickTreeListItem, false);
- }
- }
-
- // If it is an history
- if (treeList[i].id) {
- var title = treeList[i].name;
- if(treeList[i].pearlCount >= MAX_PEARLS_IN_TREE) {
- title = title + " (full)";
- }
- item = this.appendItemToListBox(title, treeList[i], true);
- item.addEventListener('click', BRO_inButtonController.onClickHistoryListItem, false);
- }
-
- // Set specific display if the item selected is full
- if(item) {
- if(treeList[i].pearlCount >= MAX_PEARLS_IN_TREE) {
- item.className = 'BRO_treeFull';
- }else{
- item.className = null;
- }
- }
-
- // We update the selected tree if needed
- // @todo remove
- if (this.getSelectedTree() && this.getSelectedTree().treeID == treeList[i].treeID) {
- isSelectedTreeInList = true;
- if (this.getSelectedTree().title != treeList[i].title) {
- this._selectedTree = treeList[i];
- this.refreshTreeListVisualComponents(BRO_toolbar.isRecording);
- }
- }
- }
-
- // Find user dropzone tree and show it at the bottom of the list
- // if(BRO_toolbar.getRecordingMode() == RECORING_MODE_ONE_BY_ONE) {
- //
- // // Add space separator
- // treeListBox.appendChild(this.createTreeListLineSeparator());
- //
- // if(currentUser && currentUser.dropZoneID) {
- // for(var i=0;i<listLength;i++) {
- // if(treeList[i].treeID == currentUser.dropZoneID) {
- // treeList[i].title = BRO_locale.getString('treeList.dropzone');
- // var item = this.appendItemToListBox(treeList[i].title, treeList[i],
- // true);
- // item.style.fontWeight = 'bold';
- // item.addEventListener('click',
- // BRO_inButtonController.onClickTreeListItem, false);
- // break;
- // }
- // }
- // }
- // }
-
- // Add space separator
- // treeListBox.appendChild(this.createTreeListSpacerSeparator(8));
-
- // If the selected tree is no longer in the list we select the default
- // tree
- // @todo remove
- if (this.getSelectedTree() && !isSelectedTreeInList) {
- this.selectRootTree();
- }
-
- this.refreshTreeListHeight();
- },
-
- sortTreeListItemByTitle : function(a, b) {
- // a is a tree
- if (a.treeID) {
- var aTitle = a.title.toLowerCase();;
- }
- // a is an history
- else if (a.id) {
- var aTitle = a.name.toLowerCase();;
- } else {
- return;
- }
-
- // b is a tree
- if (b.treeID) {
- var bTitle = b.title.toLowerCase();;
- }
- // b is an history
- else if (b.id) {
- var bTitle = b.name.toLowerCase();;
- } else {
- return;
- }
-
- return ((aTitle < bTitle) ? -1 : ((aTitle > bTitle) ? 1 : 0));
- },
-
- appendItemToListBox : function(title, value, encode) {
- var treeListBox = document.getElementById('BRO_treeListBox');
- if (!treeListBox)
- return null;
- if (encode) {
- value = BRO_model._json.encode(value);
- }
- return treeListBox.appendItem(title, value);
- },
-
- decodeListBoxItemValue : function(listItem) {
- if (!listItem || !listItem.value || listItem.value == 'undefined')
- return null;
- return BRO_model._json.decode(listItem.value);
- },
-
- /**
- * Fix the popup height if it is bigger than the window height
- *
- * We calculate the popup size because Firefox does not do it well. TODO
- * find a better way to calculate the popup size.
- */
- refreshTreeListHeight : function() {
- var treeListBox = document.getElementById('BRO_treeListBox');
- if(!treeListBox) return;
- var newButton = document.getElementById('BRO_newButton');
- var newButtonPopup = document.getElementById('BRO_newButtonPopup');
- if(!newButton || !newButtonPopup) return;
-
- var treeList = BRO_model.getTreeList();
- var historyList = BRO_model.getHistoryList();
- var treeLength = (treeList) ? treeList.length : 0;
- var historyLength = (historyList) ? historyList.length : 0;
- var listLength = treeLength + historyLength;
- var lineHeight = 19;
- var separatorHeight = 28;
- var borderWidth = 2;
-
- var newButtonPopupHeader = document.getElementById('BRO_newButtonPopupHeader');
- var headerHeight = separatorHeight;
- newButtonPopupHeader.setAttribute('style', 'height:' + headerHeight+ 'px');
- var listHeight = headerHeight + (lineHeight * (listLength + 2))+ separatorHeight + (borderWidth * 2);
-
- var windowHeight = window.outerHeight;
- var listY = newButton.boxObject.height + newButton.boxObject.y;
- var maxHeight = windowHeight - listY - 55;
- var maxHeight = (listHeight > maxHeight) ? maxHeight : null;
- newButtonPopup.setAttribute('maxheight', maxHeight);
- },
-
- createTreeListSpacerSeparator : function(spacerHeight) {
- if (!spacerHeight || spacerHeight < 1)
- spacerHeight = 15; // By default 1 line height
- var item = document.createElement('richlistitem');
- item.className = 'BRO_treeListSeparatorItem';
- item.setAttribute('selectable', 'false');
- var separator = document.createElement('spacer');
- separator.height = spacerHeight;
- item.appendChild(separator);
- return item;
- },
-
- createTreeListTextSeparator : function(text) {
- var item = document.createElement('richlistitem');
- item.className = 'BRO_treeListSeparatorItem';
- item.setAttribute('selectable', 'false');
- item.style.fontWeight = 'bold';
- // var separator = document.createTextNode(text);
- var separator = document.createElement('description');
- var textNode = document.createTextNode(text);
- separator.appendChild(textNode);
- item.appendChild(separator);
- return item;
- },
-
- createTreeListLineSeparator : function() {
- var item = document.createElement('richlistitem');
- item.className = 'BRO_treeListSeparatorItem';
- item.setAttribute('selectable', 'false');
- var separator = document.createElement('image');
- separator.flex = 0;
- separator.className = 'BRO_treeListSeparator';
- item.appendChild(separator);
- return item;
- },
-
- onShowTreeList : function(event) {
- BRO_toolbar.onUseToolbar();
- // Try to back up selection
- this.backupSelection();
- // Refresh le list
- BRO_model.getTreesAndCurrentUser();
- // Refresh list size when initialized (few ms after popup shown)
- BRO_tools.callWithDelay('BRO_inButtonController.refreshTreeListHeight()', 10);
- },
-
- onKeypress : function(event) {
- var newButtonPopup = document.getElementById('BRO_newButtonPopup');
- if (newButtonPopup && (event.keyCode == event.DOM_VK_ESCAPE || event.keyCode == event.DOM_VK_CANCEL)) {
- newButtonPopup.hidePopup();
- }
- }
- }
- window.addEventListener("keypress", BRO_inButtonController.onKeypress, true);
-